package com.ulic.customerManApplication.base;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.commonmodule.service.OrganizationService;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.servlet.GetSpringBeanServlet;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.usermodule.service.PermissionService;

/**
 * @Version 1.0
 * @author 吴积嵩
 * @Date 2010-11-02
 * @description Action抽象类: 所有Action类继承该类,子类可以方便拿到jsp常用内置对象
 */
public abstract class BaseAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware {
	private static final long serialVersionUID = 1L;
	protected Map<String, Object> request;
	protected Map<String, Object> session;
	protected Map<String, Object> application;
	protected HttpServletResponse response = ServletActionContext.getResponse();
	protected int curPage = 1;
	protected int oid;
	protected int oids[];

	/**
	 * 重新缓存权限功能点等信息
	 * @throws Exception
	 */
	public void recache() throws Exception {
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
//		PermissionService permissionService = (PermissionService)GetSpringBeanServlet.getBean("permissionService");
//		session.put(CONSTANT.MENU_PERMISSON_SESSION_KEY, permissionService.getMenuPermissionList(Integer.parseInt(user.getUm_user_id())));
		String rolename = session.get("rolename").toString();
		OrganizationService organizationService = (OrganizationService)GetSpringBeanServlet.getBean("organizationService");
		if("ADMIN".equals(rolename)){//一级管理员只取26家分公司，中支后期更具不同的分公司进行处理
			session.put("barchList", organizationService.findOrganizationByParentId(organizationService.getFinalOrganization().getUm_organ_id()));
			session.put("expensedList", null);
		}else if("SUBADMIN".equals(rolename)){//二级管理员取出所在分公司后，再取出分公司下的所有中支
			Organization organ = organizationService.getBranchByUserOid(user.getOrganization().getUm_organ_id());
			session.put("barchList", organ);
			session.put("expensedList", organizationService.findOrganizationByParentId(organ.getUm_organ_id()));
		}else{//三级管理员只取出所在分公司，和中支
			Organization organ = organizationService.getBranchByUserOid(user.getOrganization().getUm_organ_id());
			session.put("barchList", organ);
			session.put("expensedList", organizationService.getExpensedByUserOid(user.getOrganization().getUm_organ_id(), organ.getUm_organ_id()));
		}
	}
	
	public void adminrecache() throws Exception {
		PermissionService permissionService = (PermissionService)GetSpringBeanServlet.getBean("permissionService");
		session.put(CONSTANT.MENU_PERMISSON_SESSION_KEY, permissionService.getAdminMenuPermissionList());
	}
	
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = ServletActionContext.getResponse();
	}
	
	public HttpServletResponse getResponse() {
		return response;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getOid() {
		return oid;
	}

	public void setOid(int oid) {
		this.oid = oid;
	}

	public int[] getOids() {
		return oids;
	}

	public void setOids(int[] oids) {
		this.oids = oids;
	}
}
